import { defineStore } from "pinia"
import { RecMaterial, RecMaterialPageSearch } from "@/models/material"
import { materialApi } from "@/services"
import { REC_TYPE } from "@/utils/constant"

const useMaterialStore  = defineStore('Material', {
    // state
    state:() =>({
        topicMaterialList: [] as RecMaterial[],
        selectedMaterialList: [] as RecMaterial[],
    }),

    // actions
    actions:{
        // 获取专题资料列表
        async initTopicMatrials(pageSize: number) {
            try {
                const data : RecMaterialPageSearch = {
                    rec: REC_TYPE.TOPIC,
                    pageSize: pageSize,
                    pageNum: 1
                };
                const res = await materialApi.getRecMaterials(data)
                this.topicMaterialList = res? res.records : [] as RecMaterial[]
            } catch(err) {
        
            }
        },
        // 获取精选资料列表
        async initSelectedMatrials(pageSize: number) {
            try {
                const data : RecMaterialPageSearch = {
                    rec: REC_TYPE.SELECTED,
                    pageSize: pageSize,
                    pageNum: 1
                };
                const res = await materialApi.getRecMaterials(data)
                this.selectedMaterialList = res? res.records : [] as RecMaterial[]
            } catch(err) {
        
            }
        },
    },
    getters:{
        topicMaterialSource:(state) => state.topicMaterialList,
        selectedMaterialSource:(state) => state.selectedMaterialList,
    }
})

export default useMaterialStore;